package com.neu.his.controller.outpatientDoctorStation03;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.neu.his.core.Result;
import com.neu.his.entity.DoctorDuty;
import com.neu.his.entity.VO.outpatientDoctorStation03.ODDutyVO;
import com.neu.his.entity.request.DeleteParams;
import com.neu.his.entity.request.oDDuty.ODDutyPageParams;
import com.neu.his.entity.request.oDDuty.ODDutyParams;
import com.neu.his.mapper.DoctorDutyMapper;
import com.neu.his.utils.PageList;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Description: neu-his
 * <p>
 * Created by 31316 on 2020/7/18 12:18
 */
@RestController
@RequestMapping("/doctor_duty")
@Slf4j
public class ODDutyController {
    @Autowired
    DoctorDutyMapper doctorDutyMapper;

    @PostMapping
    public Result insertOne(@RequestBody ODDutyParams odDutyParams) {
        if (!doctorDutyMapper.selectList(new QueryWrapper<DoctorDuty>().eq("day_of_week",
                odDutyParams.getDayOfWeek()).eq("noon", odDutyParams.getNoon())
                .ne("remove_mark", 1)).isEmpty())
            return Result.fail("已经存在");
        DoctorDuty doctorDuty = new DoctorDuty();
        doctorDuty.setDayOfWeek(odDutyParams.getDayOfWeek());
        doctorDuty.setNoon(odDutyParams.getNoon());
        doctorDuty.setOutpatientDoctorId(odDutyParams.getOutpatientDoctorId());
        doctorDuty.setDate(new Date());
        doctorDuty.setCreateDate();
        doctorDutyMapper.insert(doctorDuty);
        return Result.success();
    }

    @PostMapping("/query")
    public Result oDDutyQuery(@RequestBody ODDutyPageParams odDutyPageParams) {
        QueryWrapper<DoctorDuty> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(odDutyPageParams.getNoon() != null, "noon", odDutyPageParams.getNoon());
        queryWrapper.eq(odDutyPageParams.getDayOfWeek() != 0, "day_of_week", odDutyPageParams.getDayOfWeek());
        queryWrapper.eq(odDutyPageParams.getOutpatientDoctorId() != null, "outpatient_doctor_id", odDutyPageParams.getOutpatientDoctorId());
        queryWrapper.ne("remove_mark", 1);
        List<DoctorDuty> list = doctorDutyMapper.selectList(queryWrapper);
        List<ODDutyVO> result=new ArrayList<>();
        for (DoctorDuty doctorDuty : list) {
            ODDutyVO odDutyVO=new ODDutyVO();
            odDutyVO.setDayOfWeek(doctorDuty.getDayOfWeek());
            odDutyVO.setId(doctorDuty.getId());
            odDutyVO.setNoon(doctorDuty.isNoon());
            odDutyVO.setOutpatientDoctorI(doctorDuty.getOutpatientDoctorId());
            result.add(odDutyVO);
        }
        PageList pageList=new PageList(result,odDutyPageParams.currentPage,odDutyPageParams.pageSize);
        return Result.success(pageList);
    }

    @PutMapping
    public Result updateOne(@RequestBody ODDutyParams odDutyParams){
        DoctorDuty doctorDuty;
        while ((doctorDuty=doctorDutyMapper.selectOne(new QueryWrapper<DoctorDuty>().eq("day_of_week",
                odDutyParams.getDayOfWeek()).eq("noon", odDutyParams.getNoon())
                .ne("remove_mark", 1)))==null)
            insertOne(odDutyParams);
        //return Result.fail("不存在");
        doctorDuty.setUpdateDate();
        doctorDuty.setOutpatientDoctorId(odDutyParams.getOutpatientDoctorId());
        doctorDutyMapper.updateById(doctorDuty);
        return Result.success("修改成功");
    }

    @DeleteMapping
    public Result deletSome(@RequestBody DeleteParams deleteParams){
        UpdateWrapper<DoctorDuty> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", deleteParams.getIds());
        updateWrapper.set("remove_mark", 1);
        doctorDutyMapper.update(null, updateWrapper);
        return Result.success(new ArrayList<Long>());
    }
}
